﻿using MediatR;
using S.Produce.Api.Application.Commands.PregnantCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.PregnantCommandsHandlers
{
    /// <summary>
    /// 
    /// </summary>
    public class PregnantDeleteCommandHandler : IRequestHandler<PregnantDeleteCommand, int>
    {
        private readonly IRepository<Pregnant> _repository;

        public PregnantDeleteCommandHandler(IRepository<Pregnant> repository)
        {
            _repository = repository;
        }

        public async Task<int> Handle(PregnantDeleteCommand request, CancellationToken cancellationToken)
        {
            var ids = request.Ids.Split(",");
            var count = 0;
            foreach (var item in ids)
            {
                var del = await _repository.GetAsync(int.Parse(item));
                del.IsDel = true;
                count += await _repository.UpdateAsync(del);
            }
            return count;
        }
    }
}
